package com.collectplus.express.parcel;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.collectplus.express.BaseActivity2;
import com.collectplus.express.R;
import com.collectplus.express.logic.AppHttp;
import com.collectplus.express.logic.AppResult;
import com.collectplus.express.logic.JSON;
import com.collectplus.express.logic.MsgID;

import java.util.Map;

/**
 * 新版我的包裹主界面
 *
 * @author kaka<br>
 *         2017-1-6下午5:40:17
 */
public class ParcelMainActivity2 extends BaseActivity2 implements OnClickListener {

    private boolean isRequest = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void findViewById() {
        setContentView(R.layout.parcel_main_new);
        super.findViewById();
        getAppTitle().setCommonTitle("我的包裹");

        // 寄出的包裹
        findViewById(R.id.parcel_send_unpay).setOnClickListener(this);
        findViewById(R.id.parcel_send_unsend).setOnClickListener(this);
        findViewById(R.id.parcel_send_sended).setOnClickListener(this);
        findViewById(R.id.parcel_send_all).setOnClickListener(this);
        // 收到的包裹
        findViewById(R.id.parcel_receive_transpoting).setOnClickListener(this);
        findViewById(R.id.parcel_receive_unsign).setOnClickListener(this);
        findViewById(R.id.parcel_receive_signed).setOnClickListener(this);
        findViewById(R.id.parcel_receive_all).setOnClickListener(this);

        mSwipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh);
        mSwipeLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                AppHttp.getInstance().getParcelCount();
                AppHttp.getInstance().getReceiverParcelCount();
            }
        });
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case MsgID.parcel_count:
                cancelLoadingDialog();
                onRefreshComplete();
                final AppResult<Integer> result = JSON.parseMap(msg.obj, Integer.class);
                if (result.isSuccess()) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            handlerCount(result.getResultMap());
                        }
                    });
                } else {
                    showToast(result.getMessage());
                }
                return true;
            //收到包裹的数量
            case MsgID.receiver_parcel_count:
                cancelLoadingDialog();
                onRefreshComplete();
                final AppResult<Integer> result1 = JSON.parseMap(msg.obj, Integer.class);
                if (result1.isSuccess()) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            handlerReceiverCount(result1.getResultMap());
                        }
                    });
                } else {
                    showToast(result1.getMessage());
                }
                return true;
            default:
                return super.handleMessage(msg);
        }
    }

    protected void handlerCount(Map<String, Integer> parseGetMapValue) {
        if (parseGetMapValue == null) {
            return;
        }
        TextView unpay = (TextView) findViewById(R.id.parcel_unpay_count);
        TextView unsend = (TextView) findViewById(R.id.parcel_unsend_count);
        TextView sended = (TextView) findViewById(R.id.parcel_sended_count);
        Integer unsendCount = parseGetMapValue.get("待寄出");
        Integer unpayCount = parseGetMapValue.get("待支付");
        Integer sendedCount = parseGetMapValue.get("已寄出");
        if (unsendCount != null && unsendCount > 0) {
            unsend.setText("" + unsendCount);
            unsend.setVisibility(View.VISIBLE);
        } else {
            unsend.setVisibility(View.GONE);
        }
        if (unpayCount != null && unpayCount > 0) {
            unpay.setText("" + unpayCount);
            unpay.setVisibility(View.VISIBLE);
        } else {
            unpay.setVisibility(View.GONE);
        }
        if (sendedCount != null && sendedCount > 0) {
            sended.setText("" + sendedCount);
            sended.setVisibility(View.VISIBLE);
        } else {
            sended.setVisibility(View.GONE);
        }
    }

    protected void handlerReceiverCount(Map<String, Integer> parseGetMapValue) {
        if (parseGetMapValue == null) {
            return;
        }
        TextView transpoting = (TextView) findViewById(R.id.parcel_receive_transpoting_count);
        TextView reachedStation = (TextView) findViewById(R.id.parcel_receive_unsign_count);
        TextView signed = (TextView) findViewById(R.id.parcel_receive_signed_count);
        Integer transpotingCount = parseGetMapValue.get("wayCount");
        Integer reachedStationCount = parseGetMapValue.get("stationCount");
        Integer signedCount = parseGetMapValue.get("signedCount");
        if (transpotingCount != null && transpotingCount > 0) {
            transpoting.setText("" + transpotingCount);
            transpoting.setVisibility(View.VISIBLE);
        } else {
            transpoting.setVisibility(View.GONE);
        }
        if (reachedStationCount != null && reachedStationCount > 0) {
            reachedStation.setText("" + reachedStationCount);
            reachedStation.setVisibility(View.VISIBLE);
        } else {
            reachedStation.setVisibility(View.GONE);
        }
        if (signedCount != null && signedCount > 0) {
            signed.setText("" + signedCount);
            signed.setVisibility(View.VISIBLE);
        } else {
            signed.setVisibility(View.GONE);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.parcel_send_unpay:
            case R.id.parcel_send_unsend:
            case R.id.parcel_send_sended:
            case R.id.parcel_send_all:
                Intent intent = new Intent(this, ParcelSendActivity.class);
                intent.putExtra("item", Integer.valueOf(v.getTag() + ""));
                startActivity(intent);
                break;
            case R.id.parcel_receive_transpoting:
            case R.id.parcel_receive_unsign:
            case R.id.parcel_receive_signed:
            case R.id.parcel_receive_all:
                Intent intent2 = new Intent(this, ParcelReceiveActivity.class);
                intent2.putExtra("item", Integer.valueOf(v.getTag() + ""));
                startActivity(intent2);
                break;
            default:
                break;
        }

    }

    @Override
    protected void onStop() {
        isRequest = true;
        super.onStop();
    }

    @Override
    protected void onResume() {
        if (isRequest) {
            AppHttp.getInstance().getParcelCount();
            AppHttp.getInstance().getReceiverParcelCount();
        }
        isRequest = false;
        super.onResume();
    }
}
